package com.wzx.ptplatform.controller.admin.user;

import com.wzx.ptplatform.domain.City;
import com.wzx.ptplatform.domain.University;
import com.wzx.ptplatform.service.UniversityService;
import com.wzx.ptplatform.utils.ConstUtils;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员用户模块——学校信息管理")
@RestController
public class AdminUserUniversityController {

    final UniversityService universityService;

    public AdminUserUniversityController(UniversityService universityService) {
        this.universityService = universityService;
    }

    @RequestMapping("admin_user_university_message_list")
    public LayuiPager<University> adminUserStudentMessageList(
            String universityName, String cityName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        University university = new University();
        if (universityName != null && !"".equals(universityName)) {
            university.setUniversityName(universityName);
        }
        if (cityName != null && !ConstUtils.PLEASE_CHOOSE.equals(cityName)) {
            university.setCity(new City(Long.parseLong(cityName)));
        }
        Page<University> pager = universityService.findList(university, page - 1, limit);
        LayuiPager<University> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_user_university_message_addView")
    public ModelAndView adminUserUniversityMessageAddView() {
        return new ModelAndView("admin/user/university_message_add");
    }

    @GetMapping("admin_user_university_message_editView")
    public ModelAndView adminUserUniversityMessageEditView(String id, Model model) {
        model.addAttribute("university", universityService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/user/university_message_edit");
    }

    @PostMapping("admin_user_university_message_save_or_edit")
    public Map<String, Object> adminUserUniversityMessageSaveOrEdit(University university) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (universityService.saveOrEdit(university)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_university_message_delete")
    public Map<String, Object> adminUserUniversityMessageDelete(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (universityService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_user_university_message_delete_select")
    public Map<String, Object> adminUserUniversityMessageDeleteSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    universityService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
